#include <stdio.h>
#include <stdlib.h>
#include <athread.h>

volatile int main_test=1;
unsigned long over_addr[64]={0};

#define my_h2ldm(element_addr, speid, cgid, type)  *(typeof(type)*)SPEIO_LDM_ADDR(cgid,speid,element_addr)
#define SERVER 0

extern SLAVE_FUN(test_remote());

int main(int argc,char **argv){
  printf("-------begin-------\n");
  athread_init();
  int threads=64;
  int i;



  int cgid=current_array_id();  //核组号

  if(argc>1)
    threads=atoi(argv[1]);

  for(i=0;i<threads;++i){
    athread_create(i,test_remote,0);
  }

  while(1)
  {
    int over_count=0;
    for(i=0;i<threads;i++)
    {
      if(i==0) continue;
      over_count+=my_h2ldm(over_addr[i],i,cgid,int); //从核全部完成
    }
    printf("%d,",over_count);
    if(over_count==threads-1)
    {
      my_h2ldm(over_addr[0],0,cgid,int)=1;
      //my_h2ldm(over_addr[0],0,cgid,int)=1;
      break;
    }
  }
 

  printf("------master-end-------\n"); 
  for( i = 0; i <threads; ++i){
    athread_wait(i);
    athread_end(i);
  }
  printf("-------end-------\n"); 

}
//运行结果
// -------begin-------
// 3,63,------master-end-------

// upper=2016, sum=2016
// ,-------end-------
